﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

namespace ProjectManageSystemApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    [Authorize]
    public class RoleController : Controller
    {
        readonly IRoleMenuServices _role;
        readonly IUserServices _user;
        public RoleController(IRoleMenuServices role, IUserServices user) 
        {
            _role = role;
            _user = user;
        }

        /// <summary>
        /// 用户角色绑定
        /// </summary>
        [HttpPost, Route("BindUserRole")]
        [Authorize]
        public ApiResult BindUserRole([FromBody]UserRoleRequest userRole)
        {
            if (userRole == null || userRole.UserId == 0 || userRole.RoleId.Count <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            // 判断用户是否存在
            if (!_user.ExistByUserId(userRole.UserId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }

            if (!_role.BindUserRole(userRole.UserId, userRole.RoleId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }


        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("GetAllRole")]
        [Authorize]
        public ApiResult GetAllRole([FromBody]Models.PageBo<RoleParam> pageBo)
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<RoleParam>();
            }
            if (pageBo.Param == null) 
            {
                pageBo.Param = new RoleParam();
            }
            var data = _role.GetAll(pageBo);
            return ApiResultStatus.Success(data);
        }

        /// <summary>
        /// 获取用户的角色权限
        /// </summary>
        /// <returns></returns>
        [HttpGet,Route("GetRoleAuth")]
        public ApiResult GetRoleAuth(long roleId) 
        {
            return ApiResultStatus.Success(_role.GetRoleAuth(roleId));
        }
        /// <summary>
        /// 修改用户权限
        /// </summary>
        /// <returns></returns>
        [HttpPost,Route("UpdRoleAuth")]
        public ApiResult UpdRoleAuth(UpdRoleAuthResponse roleAuth)
        {
            return ApiResultStatus.Create(_role.UpdRoleAuth(roleAuth), ApiResultCodes.OPERATION_FAILED);
        }

    }

}

namespace ProjectManageSystemApi.Models 
{
    /// <summary>
    /// 修改角色权限请求参数
    /// </summary>
    public class UpdRoleAuthResponse 
    {
        public long RoleId { get; set; }
        public SysCoreRoleExtraDto RoleExtra { get; set; }
        public List<long> MenuIdLists { get; set; } = new List<long>();
        public List<long> ElementIdLists { get; set; } = new List<long>();

    }
}
